﻿using System;
using System.Collections.Generic;
using System.Collections;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data;
using System.IO;
using System.Text;

using CE.User;
using CE.Global;
using CE.Provider;
using CE.Database;
using CE.CarInsurance;

public partial class Provider_ProposalForm_DownloadProposalForm : System.Web.UI.Page
{
    string PageName = "CXDPF";
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session[CE.Global.General.UserId] == null || Session[CE.Global.General.ProviderCompanyId] == null)
        {
            Response.Redirect("~/Provider/ProviderSecureLogin.aspx");
        }
        CheckMainPageAccess();

        if (!IsPostBack)
        {
            CheckSubPageAccessByInsuranceType();
            ddlDay1.Visible = false;
            lblFromDate.Text = "Date :";
            lblToDate.Visible = false;
            ddlDay2.Visible = false;
            ddlMonth2.Visible = false;
            ddlYear2.Visible = false;

            ddlYear1.Items.Clear();
            ddlYear2.Items.Clear();
            int _year = CE.Global.General.GetUTCDateTime().Year;
            ddlYear1.Items.Add(new ListItem(_year.ToString(), _year.ToString()));
            ddlYear2.Items.Add(new ListItem(_year.ToString(), _year.ToString()));
            _year = _year - 1;
            ddlYear1.Items.Add(new ListItem(_year.ToString(), _year.ToString()));
            ddlYear2.Items.Add(new ListItem(_year.ToString(), _year.ToString()));

            //ddlDay1.Text = "00";
            ddlDay1.Text = CE.Global.General.GetUTCDateTime().Day.ToString("00");
            ddlMonth1.Text = CE.Global.General.GetUTCDateTime().Month.ToString("00");
            ddlYear1.Text = CE.Global.General.GetUTCDateTime().Year.ToString("0000");
            //ddlDay2.Text = "00";
            ddlDay2.Text = CE.Global.General.GetUTCDateTime().Day.ToString("00");
            ddlMonth2.Text = CE.Global.General.GetUTCDateTime().Month.ToString("00");
            ddlYear2.Text = CE.Global.General.GetUTCDateTime().Year.ToString("0000");

            ImageButton1_Click(this, null);

            if (Session[CE.Global.General.ProviderCompanyId].ToString() == "23")
            {
                ddlFileType.Visible = true;
            }
        }
    }

    protected void ImageButton1_Click(object sender, EventArgs e)
    {
        
        txtReportDate1.Text = "";
        txtReportDate2.Text = CE.Global.General.GetUTCDateTime().Year.ToString("0000") + CE.Global.General.GetUTCDateTime().Month.ToString("00") + CE.Global.General.GetUTCDateTime().Day.ToString("00");
        txtReportDate1.Text = ddlYear1.Text;
        txtReportDate1.Text += ddlMonth1.Text;
        if (ddlReportType.SelectedIndex > 0)
        {
            if (ddlDay1.SelectedIndex > 0)
            {
                txtReportDate1.Text += ddlDay1.Text;
            }
        }
        if (ddlReportType.SelectedIndex == 2)
        {
            txtReportDate2.Text = ddlYear2.Text;
            txtReportDate2.Text += ddlMonth2.Text;
            txtReportDate2.Text += ddlDay2.Text;
        }

        ProposalDownloadDB _proposaldb = new ProposalDownloadDB();
        DataSet dsProposal = new DataSet();

        string ProviderCompanyId = Session[CE.Global.General.ProviderCompanyId].ToString();
        Int16 _ReportType = Convert.ToInt16(ddlReportType.Text);
        string _ReportDate1 = txtReportDate1.Text.Trim();
        string _ReportDate2 = txtReportDate2.Text.Trim();
        
        //if (ddlInsuranceType.Text == "PA" && ProviderCompanyId == "30")
        //{
        //    string _SubmissionType = ddlSubmissionType.Text;
        //    dsProposal = _proposaldb.GetEtiqaPAProposalSummary(Convert.ToInt64(ProviderCompanyId.ToString()), ddlInsuranceType.Text, _ReportType, _ReportDate1, _ReportDate2, "0", 0, _SubmissionType);
        //    grvEtiqaPAProposal.DataSource = dsProposal;
        //    grvEtiqaPAProposal.DataBind();
        //}
        //else
        //{
        //    dsProposal = _proposaldb.GetAllProposal(ProviderCompanyId, ddlInsuranceType.Text, _ReportType, _ReportDate1, _ReportDate2);
        //    grvResultViewProposal.DataSource = dsProposal;
        //    grvResultViewProposal.DataBind();
        //}
        dsProposal = _proposaldb.GetAllProposal(ProviderCompanyId, ddlInsuranceType.Text, _ReportType, _ReportDate1, _ReportDate2);
        grvResultViewProposal.DataSource = dsProposal;
        grvResultViewProposal.DataBind();
        grvEtiqaPAProposal.DataSource = dsProposal;
        grvEtiqaPAProposal.DataBind();
    
        if (ddlInsuranceType.Text == "PA" && ProviderCompanyId == "30")
        {
            grvResultViewProposal.Visible = false;
            grvEtiqaPAProposal.Visible = true;
            btnDownLoad.Visible = true;
        }
        else
        {
            grvResultViewProposal.Visible = true;
            grvEtiqaPAProposal.Visible = false;
            btnDownLoad.Visible = false;
        }

    }

    protected void ibtBack_Click(object sender, ImageClickEventArgs e)
    {
        Response.Redirect("~/Provider/ProviderHome.aspx");
    }

    protected void ConfirmDownload(object sender, ImageClickEventArgs e)
    {
        string refid = inputrefid.Value;
        ProposalDownloadDB _proposaldb = new ProposalDownloadDB();
        DataSet dsDownload = new DataSet();

        Int64 ProviderCompanyId = Convert.ToInt64(Session[CE.Global.General.ProviderCompanyId].ToString());
        Int64 ResultId = 0;
        //string isconfirmdownload = "False";
        Boolean isconfirmdownload = false;

        if (ddlInsuranceType.Text == "CI")
        {
            dsDownload = _proposaldb.GetCarDownloadInfo(Convert.ToInt64(refid), Convert.ToInt64(Session[CE.Global.General.ProviderCompanyId].ToString()));
        }
        else if (ddlInsuranceType.Text == "TI")
        {
            dsDownload = _proposaldb.GetTIDownloadInfo(Convert.ToInt64(refid), Convert.ToInt64(Session[CE.Global.General.ProviderCompanyId].ToString()));
        }
        else if (ddlInsuranceType.Text == "PA")
        {
            dsDownload = _proposaldb.GetPADownloadInfo(Convert.ToInt64(refid), Convert.ToInt64(Session[CE.Global.General.ProviderCompanyId].ToString()));
        }

        if (dsDownload.Tables[0].Rows.Count > 0)
        {
            isconfirmdownload = Boolean.Parse(dsDownload.Tables[0].Rows[0]["IsConfirmDownload"].ToString());
        }

        Int64 UserId = Convert.ToInt64(Session[CE.Global.General.UserId].ToString());

        if (ddlInsuranceType.Text == "CI")
        {
            ResultId = _proposaldb.UpdateCarDownloadInfo(Convert.ToInt64(refid), ProviderCompanyId, true, isconfirmdownload, 1, 0, UserId);
        }
        else if (ddlInsuranceType.Text == "TI")
        {
            ResultId = _proposaldb.UpdateTIDownloadInfo(Convert.ToInt64(refid), ProviderCompanyId, true, isconfirmdownload, 1, 0, UserId);
        }
        else if (ddlInsuranceType.Text == "PA")
        {
            ResultId = _proposaldb.UpdatePADownloadInfo(Convert.ToInt64(refid), ProviderCompanyId, true, isconfirmdownload, 1, 0, UserId);
        }

        if (!isconfirmdownload && ProviderCompanyId != 30)
        {
            Response.Redirect("DownloadProposalConfirmation.aspx?pid=" + refid + "&type=" + ddlInsuranceType.Text + "&dl=" + ResultId.ToString());
        }


    }

    public string GetReferenceNumber(string Number)
    {
        string convertno = General.GetRefNumber(Convert.ToInt64(Number.ToString()));
        string _RefNo = "";
        if (Session[CE.Global.General.ProviderCompanyId].ToString() == "23")
        {
            if (ddlInsuranceType.Text == "PA")
            {
                _RefNo = "PAH" + convertno.ToString();
            }
            else if (ddlInsuranceType.Text == "TI")
            {
                _RefNo = "TIH" + convertno.ToString();
            }
            else
            {
                _RefNo = "H" + convertno.ToString();
            }
        }
        else if (Session[CE.Global.General.ProviderCompanyId].ToString() == "30")
        {
            _RefNo = "E" + convertno.ToString();
        }
        else if (Session[CE.Global.General.ProviderCompanyId].ToString() == "29")
        {
            if (ddlInsuranceType.Text == "PA")
            {
                _RefNo = "PAL" + convertno.ToString();
            }
            else if (ddlInsuranceType.Text == "TI")
            {
                _RefNo = "TIL" + convertno.ToString();
            }
            else
            {
                _RefNo = "L" + convertno.ToString();
            }
        }
        else if (Session[CE.Global.General.ProviderCompanyId].ToString() == "24")
        {
            if (ddlInsuranceType.Text == "TI")
            {
                _RefNo = "TIR" + convertno.ToString();
            }
            else
            {
                _RefNo = "R" + convertno.ToString();
            }
        }
        return _RefNo;
    }

    public string GetDownloadURl(string refid)
    {

        //if (Session[General.ProviderCompanyId].ToString() == "23")
        //{
        //    if (ddlInsuranceType.Text == "CI")
        //    {
        //        _html = GenerateCIHTML(Convert.ToInt64(refid.ToString()));
        //    }
        //}
        string _url = "";
        Session[General.InsType] = ddlInsuranceType.Text;
        string _refId = CE.Security.MySecurity.Encrypt(refid.ToString(), true);
        _url = "javascript:OpenProposal('" + refid.ToString() + "');";
        return _url;
    }

    public string GetPDFDownloadURl(string refid)
    {
        string _url = "";
        Session[General.InsType] = ddlInsuranceType.Text;
        string _refId = CE.Security.MySecurity.Encrypt(refid.ToString(), true);
        _url = "javascript:OpenProposalPDF('" + refid.ToString() + ",pdf');";
        return _url;
    }

    public string GetBoolValue(string bval)
    {
        string boolvalue = "No";

        if (bval == "True")
            boolvalue = "Yes";

        return boolvalue;
    }

    protected void ddlInsuranceType_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (Session[CE.Global.General.ProviderCompanyId].ToString() == "23")
        {
            if (ddlInsuranceType.Text == "CI")
            {
                ddlFileType.Visible = true;
            }
            else
            {
                ddlFileType.Visible = false;
            }
        }
        //if (Session[CE.Global.General.ProviderCompanyId].ToString() == "30")
        //{
        //    if (ddlInsuranceType.Text == "PA")
        //    {
        //      ddlSubmissionType.Visible = true;
        //    }
        //    else
        //    {
        //      ddlSubmissionType.Visible = false;
        //    }
        //}
    }

    protected void ddlReportType_SelectedIndexChanged(object sender, EventArgs e)
    {
        //ddlDay1.SelectedIndex = 0;
        //ddlDay2.SelectedIndex = 0;
        ddlDay1.Text = CE.Global.General.GetUTCDateTime().Day.ToString("00");
        ddlDay2.Text = CE.Global.General.GetUTCDateTime().Day.ToString("00");
        ddlMonth1.Text = CE.Global.General.GetUTCDateTime().Month.ToString("00");
        ddlMonth2.Text = CE.Global.General.GetUTCDateTime().Month.ToString("00");
        ddlYear1.Text = CE.Global.General.GetUTCDateTime().Year.ToString("0000");
        ddlYear2.Text = CE.Global.General.GetUTCDateTime().Year.ToString("0000");
        if (Convert.ToInt16(ddlReportType.Text) == 0)
        {
            ddlDay1.Visible = false;
            lblFromDate.Text = "Date :";
            lblToDate.Visible = false;
            ddlDay2.Visible = false;
            ddlMonth2.Visible = false;
            ddlYear2.Visible = false;
        }
        else if (Convert.ToInt16(ddlReportType.Text) == 1)
        {
            ddlDay1.Visible = true;
            lblFromDate.Text = "Date :";
            lblToDate.Visible = false;
            ddlDay2.Visible = false;
            ddlMonth2.Visible = false;
            ddlYear2.Visible = false;
        }
        else if (Convert.ToInt16(ddlReportType.Text) == 2)
        {
            lblFromDate.Text = "From Date :";
            lblToDate.Text = " To Date :";
            lblToDate.Visible = true;
            ddlDay2.Visible = true;
            ddlMonth2.Visible = true;
            ddlYear2.Visible = true;
            ddlDay1.Visible = true;
        }
    }

    protected void grvResultViewProposal_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        Boolean _DownLoadStatus = CheckSubPageAccess();
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            ImageButton imgDownload = new ImageButton();
            imgDownload = (ImageButton)e.Row.FindControl("ibtnDownload");
            ImageButton imgPDFDownload = new ImageButton();
            imgPDFDownload = (ImageButton)e.Row.FindControl("ibtnPDFDownload");

            if (Convert.ToInt64(Session[CE.Global.General.ProviderCompanyId].ToString()) == 23 && ddlInsuranceType.Text == "CI")
            {
                if (ddlFileType.Text == "PDF")
                {
                    imgDownload.Visible = false;
                    imgPDFDownload.Visible = true;
                }
                else
                {
                    imgDownload.Visible = true;
                    imgPDFDownload.Visible = false;
                }
            }
            else
            {
                imgDownload.Visible = true;
                imgPDFDownload.Visible = false;
            }

            if (_DownLoadStatus == true)
            {
                DateTime datePosted = DateTime.Parse(DataBinder.Eval(e.Row.DataItem, "PostedDate").ToString());

                if (datePosted < CE.Global.General.GetUTCDateTime().AddDays(-30))
                {
                    imgDownload.Enabled = false;
                }
            }
            else
            {
                imgDownload.Enabled = false;
            }
        }
    }

    protected void grvEtiqaPAProposal_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        Boolean _DownLoadStatus = CheckSubPageAccess();
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            btnDownLoad.Visible = true;
             
            if (_DownLoadStatus == true)
            {
                DateTime datePosted = DateTime.Parse(DataBinder.Eval(e.Row.DataItem, "PostedDate").ToString());

                if (datePosted < CE.Global.General.GetUTCDateTime().AddDays(-30))
                {
                    btnDownLoad.Visible = false;
                }
            }
            else
            {
                btnDownLoad.Visible = false;
            }
        }
    }

    private void CheckMainPageAccess()
    {
        Int64 PageAccess = General.GetProviderMainPageAccess(Convert.ToInt64(Session[General.ProviderCompanyId].ToString()),
                                                        Convert.ToInt64(Session[General.UserId].ToString()), PageName);
        if (PageAccess == 0)
        {
            //Response.Redirect("~/Provider/UserMessage.aspx?errorcode=2000");
            Response.Redirect("~/Provider/UserMessage.aspx?mt=UM&ec=2000");
        }
    }

    private void CheckSubPageAccessByInsuranceType()
    {
        Int64 ProviderCompanyId = Convert.ToInt64(Session[General.ProviderCompanyId].ToString());
        Int64 RefCEUserId = Convert.ToInt64(Session[General.UserId].ToString());
        MyDatabase _db = new MyDatabase();
        string _spName = "sprocProviderProposalDownloadSubPagesSelectItemListByUser";

        SortedList _spara = new SortedList();
        _spara.Add("@ProviderCompanyId", ProviderCompanyId);
        _spara.Add("@RefCEUserId", RefCEUserId);

        DataTable _dt = _db.GetDataTableBySP(_spName, _spara);
        if (_dt != null && _dt.Rows.Count > 0)
        {
            string[] _InsType = new string[_dt.Rows.Count];
            string[] _Access = new string[_dt.Rows.Count];
            for (int i = 0; i < _dt.Rows.Count; i++)
            {
                _InsType[i] = _dt.Rows[i]["CXSubPageName"].ToString();
                _Access[i] = _dt.Rows[i]["AccessLevel"].ToString();
            }
            int _InsTypeCount = 0;
            for (int j = 0; j < _InsType.Length; j++)
            {
                if (_Access[j].ToString() != "0")
                {
                    _InsTypeCount = _InsTypeCount + 1;
                }
            }
            if (_InsTypeCount == 0)
            {
                //Response.Redirect("~/Provider/UserMessage.aspx?errorcode=2000");
                Response.Redirect("~/Provider/UserMessage.aspx?mt=UM&ec=2000");
            }
            ddlInsuranceType.Items.Clear();
            for (int k = 0; k < _InsType.Length; k++)
            {
                int _index = ddlInsuranceType.Items.Count;
                if (_InsType[k].ToString() == "CXDPFCI" && _Access[k].ToString() != "0")
                {
                    ddlInsuranceType.Items.Insert(_index, new ListItem("Car Insurance", "CI"));
                }
                else if (_InsType[k].ToString() == "CXDPFTI" && _Access[k].ToString() != "0")
                {
                    ddlInsuranceType.Items.Insert(_index, new ListItem("Travel Insurance", "TI"));
                }
                else if (_InsType[k].ToString() == "CXDPFPA" && _Access[k].ToString() != "0")
                {
                    ddlInsuranceType.Items.Insert(_index, new ListItem("Personal Accident", "PA"));
                }
            }
        }
        else
        {
            //Response.Redirect("~/Provider/UserMessage.aspx?errorcode=2000");
            Response.Redirect("~/Provider/UserMessage.aspx?mt=UM&ec=2000");
        }
    }

    private bool CheckSubPageAccess()
    {
        Boolean _download = false;
        string _subPage = "";
        if (ddlInsuranceType.Text == "CI")
        {
            _subPage = "CXDPFCI";
        }
        else if (ddlInsuranceType.Text == "TI")
        {
            _subPage = "CXDPFTI";
        }
        else if (ddlInsuranceType.Text == "PA")
        {
            _subPage = "CXDPFPA";
        }
        Int64 PageAccess = General.GetProviderSubPageAccess(Convert.ToInt64(Session[General.ProviderCompanyId].ToString()),
                                                        Convert.ToInt64(Session[General.UserId].ToString()), _subPage);
        if (PageAccess == 400)
        {
            _download = true;
        }
        return _download;
    }

    protected void btnDownLoad_Click(object sender, EventArgs e)
    {
        string _RefIdList = "";
        if (grvEtiqaPAProposal.Rows.Count > 0)
        {
            for (int i = 0; i < grvEtiqaPAProposal.Rows.Count; i++)
            {
                GridViewRow row = grvEtiqaPAProposal.Rows[i];
                bool isChecked = ((CheckBox)row.FindControl("chkSelect")).Checked;
                if (isChecked)
                {
                    _RefIdList = _RefIdList + grvEtiqaPAProposal.Rows[i].Cells[7].Text + ",";
                }
            }
            if (_RefIdList.Length > 0)
            {
                string _comma = _RefIdList.Substring(_RefIdList.Length - 1, 1);
                if (_comma == ",")
                {
                    _RefIdList = _RefIdList.Substring(0, _RefIdList.Length - 1);
                }
                //GeneratePAHTML_Etiqa(_RefIdList, "PA");

                string FileName =  "PAProposalForm.aspx?Ref=" + _RefIdList;
                StringBuilder Script = new StringBuilder();
                Script.Append("<script language='javascript'>");
                Script.Append("window.open('" + FileName + "','ExpenseSheet','width=400,height=300,menubar=no, titlebar=0,");
                Script.Append("status=no,location=no,left='+((screen.width -650) / 2)+',top='+ (screen.height - 610) / 2+',toolbar=no,scrollbars=No');");
                Script.Append("</script>");
                Page.RegisterStartupScript("OpenWindow", Script.ToString());

            }
        }
    }

    public string GetAgency(string Account)
    {
        string _Agency = "";
        if (Account == "000598")
        {
            _Agency = "CompareXpress";
        }
        else if (Account == "000597")
        {
            _Agency = "TeleDirect";
        }
        return _Agency;
    }

    public string GetProvider(Int64 proividerId)
    {
        string _result = "";
        if (proividerId == 23)
        {
            _result = "HSBC";
        }
        else if (proividerId == 29)
        {
            _result = "Liberty";
        }
        else if (proividerId == 30)
        {
            _result = "Etiqa";
        }
        return _result;
    }

    public string GetProviderName(string ProviderId)
    {
        string _result = "";
        MyDatabase _db = new MyDatabase();
        string _strSql = "SELECT CompanyNameDescription FROM CX_DB_Live.. tblProviderCompany WHERE ProviderCompanyId = " + ProviderId;
        _result = _db.ExecuteScalarSqlStatement(_strSql).ToString();
        return _result;
    }

    public void GeneratePAHTML_Etiqa(string _RefId, string InsuranceType)
    {

        Int32 _ProposalCount = 0;
        Label lblContent = new Label();
        string _html = "";
        string _fileName = "FileName";
        string _applicationdate = " : " + CE.Global.General.GetUTCDateTime().Day.ToString("00") + "/" + CE.Global.General.GetUTCDateTime().Month.ToString("00") + "/" + CE.Global.General.GetUTCDateTime().Year + " " + CE.Global.General.GetUTCDateTime().ToShortTimeString();
        string convertno = General.GetRefNumber(12345);
        _fileName = "EtiqaPAProposal@" + CE.Global.General.GetUTCDateTime().Day.ToString("00") + "-" + CE.Global.General.GetUTCDateTime().Month.ToString("00") + "-" + CE.Global.General.GetUTCDateTime().Year;
        string[] RefId = _RefId.Split(',');
        _ProposalCount = RefId.Length;
        double _AnnualPremium = 0.0f;

        #region variables
        string[] _AccountCode = new string[_ProposalCount];
        string[] _AcencyName = new string[_ProposalCount];
        string[] _PostDate = new string[_ProposalCount];
        string[] _ProductName = new string[_ProposalCount];
        string[] _PlanType = new string[_ProposalCount];
        string[] _PolicyNo = new string[_ProposalCount];
        string[] _PeriodFrom = new string[_ProposalCount];
        string[] _PeriodTo = new string[_ProposalCount];
        string[] _AuCode = new string[_ProposalCount];
        string[] _BeforeGST = new string[_ProposalCount];
        string[] _PhName = new string[_ProposalCount];
        string[] _PhNRIC = new string[_ProposalCount];
        string[] _PhDOB = new string[_ProposalCount];
        string[] _PhOccupaion = new string[_ProposalCount];
        string[] _PhMobileNo = new string[_ProposalCount];
        string[] _PhHouseNo = new string[_ProposalCount];
        string[] _PhHomeNo = new string[_ProposalCount];
        string[] _PhUnitNo = new string[_ProposalCount];
        string[] _PhAddress = new string[_ProposalCount];
        string[] _PostalCode = new string[_ProposalCount];
        string[] _SpouseName = new string[_ProposalCount];
        string[] _SpouseNRIC = new string[_ProposalCount];
        string[] _SpouseDOB = new string[_ProposalCount];
        string[] _SpouseOccupation = new string[_ProposalCount];
        string[] _ChildName1 = new string[_ProposalCount];
        string[] _ChildGender1 = new string[_ProposalCount];
        string[] _ChildNRIC1 = new string[_ProposalCount];
        string[] _ChildDOB1 = new string[_ProposalCount];
        string[] _ChildName2 = new string[_ProposalCount];
        string[] _ChildGender2 = new string[_ProposalCount];
        string[] _ChildNRIC2 = new string[_ProposalCount];
        string[] _ChildDOB2 = new string[_ProposalCount];
        string[] _ChildName3 = new string[_ProposalCount];
        string[] _ChildGender3 = new string[_ProposalCount];
        string[] _ChildNRIC3 = new string[_ProposalCount];
        string[] _ChildDOB3 = new string[_ProposalCount];
        string[] _ChildName4 = new string[_ProposalCount];
        string[] _ChildGender4 = new string[_ProposalCount];
        string[] _ChildNRIC4 = new string[_ProposalCount];
        string[] _ChildDOB4 = new string[_ProposalCount];
        string[] _ChildName5 = new string[_ProposalCount];
        string[] _ChildGender5 = new string[_ProposalCount];
        string[] _ChildNRIC5 = new string[_ProposalCount];
        string[] _ChildDOB5 = new string[_ProposalCount];
        #endregion

        for (int i = 0; i < RefId.Length; i++)
        {
            DataSet _dsProposal = new DataSet();
            ProposalDownloadDB _proposaldownloaddb = new ProposalDownloadDB();
            _dsProposal = _proposaldownloaddb.GetEtiqaPAProposal(Convert.ToInt64(RefId[i].ToString()));
            if (_dsProposal.Tables[0].Rows.Count > 0)
            {
                #region Values assign to variables
                string _AgentStatus = "No";
                _AgentStatus = _dsProposal.Tables[0].Rows[0]["AgentStatus"].ToString();
                if (_AgentStatus == "No")
                {
                    _AccountCode[i] = "[000598]";
                    _AcencyName[i] = GetAgency("000598");
                }
                else
                {
                    _AccountCode[i] = "[000597]";
                    _AcencyName[i] = GetAgency("000597");
                }
                DateTime _SubmitDate = Convert.ToDateTime(_dsProposal.Tables[0].Rows[0]["PostedDate"].ToString());

                string submitdate = string.Format("{0:dd}", _SubmitDate.Day.ToString("00"));
                submitdate = submitdate + "/" + string.Format("{0:MM}", _SubmitDate.Month.ToString("00"));
                submitdate = submitdate + "/" + string.Format("{0:yyyy}", _SubmitDate.Year.ToString());
                submitdate = submitdate + " " + _SubmitDate.ToShortTimeString();

                _PostDate[i] = submitdate.ToString();
                //_PostDate[i] = _dsProposal.Tables[0].Rows[0]["PostedDate"].ToString();
                _ProductName[i] = _dsProposal.Tables[0].Rows[0]["PlanName"].ToString();
                _PlanType[i] = _dsProposal.Tables[0].Rows[0]["PlanType"].ToString();
                _PolicyNo[i] = _dsProposal.Tables[0].Rows[0]["PolicyNumber"].ToString();
                _PeriodFrom[i] = _dsProposal.Tables[0].Rows[0]["StartDate"].ToString();
                _PeriodTo[i] = _dsProposal.Tables[0].Rows[0]["EndDate"].ToString();
                _AuCode[i] = _dsProposal.Tables[0].Rows[0]["AuCode"].ToString();
                _BeforeGST[i] = _dsProposal.Tables[0].Rows[0]["AnnualPremium"].ToString();
                _AnnualPremium = _AnnualPremium + Convert.ToDouble(_dsProposal.Tables[0].Rows[0]["AnnualPremium"].ToString());
                _PhName[i] = _dsProposal.Tables[0].Rows[0]["Name"].ToString();
                _PhNRIC[i] = _dsProposal.Tables[0].Rows[0]["NRICNo"].ToString();
                _PhDOB[i] = _dsProposal.Tables[0].Rows[0]["DOB"].ToString();
                _PhOccupaion[i] = _dsProposal.Tables[0].Rows[0]["Occupation"].ToString();
                _PhMobileNo[i] = _dsProposal.Tables[0].Rows[0]["MobileNo"].ToString();
                _PhHouseNo[i] = _dsProposal.Tables[0].Rows[0]["HomePhoneNo"].ToString();
                _PhHomeNo[i] = _dsProposal.Tables[0].Rows[0]["HomeNo"].ToString();
                _PhUnitNo[i] = _dsProposal.Tables[0].Rows[0]["UnitNo"].ToString();
                _PhAddress[i] = _dsProposal.Tables[0].Rows[0]["Address"].ToString();
                _PostalCode[i] = _dsProposal.Tables[0].Rows[0]["PostalCode"].ToString();

                string _SpouseStatus = _dsProposal.Tables[0].Rows[0]["SpouseStatus"].ToString();
                _SpouseName[i] = "";
                _SpouseNRIC[i] = "";
                _SpouseDOB[i] = "";
                _SpouseOccupation[i] = "";
                if (_SpouseStatus == "Yes")
                {
                    if (_dsProposal.Tables[0].Rows.Count > 0)
                    {
                        _SpouseName[i] = _dsProposal.Tables[1].Rows[0]["SpouseName"].ToString();
                        _SpouseNRIC[i] = _dsProposal.Tables[1].Rows[0]["SpouseNRICNo"].ToString();
                        _SpouseDOB[i] = _dsProposal.Tables[1].Rows[0]["SPDOB"].ToString();
                        _SpouseOccupation[i] = _dsProposal.Tables[1].Rows[0]["SpouseOccupation"].ToString();
                    }
                }
                _ChildName1[i] = "";
                _ChildGender1[i] = "";
                _ChildNRIC1[i] = "";
                _ChildDOB1[i] = "";
                _ChildName2[i] = "";
                _ChildGender2[i] = "";
                _ChildNRIC2[i] = "";
                _ChildDOB2[i] = "";
                _ChildName3[i] = "";
                _ChildGender3[i] = "";
                _ChildNRIC3[i] = "";
                _ChildDOB3[i] = "";
                _ChildName4[i] = "";
                _ChildGender4[i] = "";
                _ChildNRIC4[i] = "";
                _ChildDOB4[i] = "";
                _ChildName5[i] = "";
                _ChildGender5[i] = "";
                _ChildNRIC5[i] = "";
                _ChildDOB5[i] = "";

                Int16 _NoofChilds = Convert.ToInt16(_dsProposal.Tables[0].Rows[0]["NoOfChild"].ToString());
                if (_dsProposal.Tables[2].Rows.Count > 0)
                {
                    for (int k = 0; k < _NoofChilds; k++)
                    {
                        if (k == 0)
                        {
                            _ChildName1[i] = _dsProposal.Tables[2].Rows[k]["Name"].ToString();
                            _ChildGender1[i] = _dsProposal.Tables[2].Rows[k]["Gender"].ToString();
                            _ChildNRIC1[i] = _dsProposal.Tables[2].Rows[k]["NRICNo"].ToString();
                            _ChildDOB1[i] = _dsProposal.Tables[2].Rows[k]["CDOB"].ToString();
                        }
                        if (k == 1)
                        {
                            _ChildName2[i] = _dsProposal.Tables[2].Rows[k]["Name"].ToString();
                            _ChildGender2[i] = _dsProposal.Tables[2].Rows[k]["Gender"].ToString();
                            _ChildNRIC2[i] = _dsProposal.Tables[2].Rows[k]["NRICNo"].ToString();
                            _ChildDOB2[i] = _dsProposal.Tables[2].Rows[k]["CDOB"].ToString();
                        }
                        if (k == 2)
                        {
                            _ChildName3[i] = _dsProposal.Tables[2].Rows[k]["Name"].ToString();
                            _ChildGender3[i] = _dsProposal.Tables[2].Rows[k]["Gender"].ToString();
                            _ChildNRIC3[i] = _dsProposal.Tables[2].Rows[k]["NRICNo"].ToString();
                            _ChildDOB3[i] = _dsProposal.Tables[2].Rows[k]["CDOB"].ToString(); ;
                        }
                        if (k == 3)
                        {
                            _ChildName4[i] = _dsProposal.Tables[2].Rows[k]["Name"].ToString();
                            _ChildGender4[i] = _dsProposal.Tables[2].Rows[k]["Gender"].ToString();
                            _ChildNRIC4[i] = _dsProposal.Tables[2].Rows[k]["NRICNo"].ToString();
                            _ChildDOB4[i] = _dsProposal.Tables[2].Rows[k]["CDOB"].ToString(); ;
                        }
                        if (k == 4)
                        {
                            _ChildName5[i] = _dsProposal.Tables[2].Rows[k]["Name"].ToString();
                            _ChildGender5[i] = _dsProposal.Tables[2].Rows[k]["Gender"].ToString();
                            _ChildNRIC5[i] = _dsProposal.Tables[2].Rows[k]["NRICNo"].ToString();
                            _ChildDOB5[i] = _dsProposal.Tables[2].Rows[k]["CDOB"].ToString();
                        }
                    }
                }
                #endregion
            }
            else
            {
                #region Assign Empty Value
                _AccountCode[i] = "";
                _AcencyName[i] = "";
                _PostDate[i] = "";
                _ProductName[i] = "";
                _PlanType[i] = "";
                _PolicyNo[i] = "";
                _PeriodFrom[i] = "";
                _PeriodTo[i] = "";
                _AuCode[i] = "";
                _BeforeGST[i] = "";
                _PhName[i] = "";
                _PhNRIC[i] = "";
                _PhDOB[i] = "";
                _PhOccupaion[i] = "";
                _PhMobileNo[i] = "";
                _PhHouseNo[i] = "";
                _PhHomeNo[i] = "";
                _PhUnitNo[i] = "";
                _PhAddress[i] = "";
                _PostalCode[i] = "";
                _SpouseName[i] = "";
                _SpouseNRIC[i] = "";
                _SpouseDOB[i] = "";
                _SpouseOccupation[i] = "";
                _ChildName1[i] = "";
                _ChildGender1[i] = "";
                _ChildNRIC1[i] = "";
                _ChildDOB1[i] = "";
                _ChildName2[i] = "";
                _ChildGender2[i] = "";
                _ChildNRIC2[i] = "";
                _ChildDOB2[i] = "";
                _ChildName3[i] = "";
                _ChildGender3[i] = "";
                _ChildNRIC3[i] = "";
                _ChildDOB3[i] = "";
                _ChildName4[i] = "";
                _ChildGender4[i] = "";
                _ChildNRIC4[i] = "";
                _ChildDOB4[i] = "";
                _ChildName5[i] = "";
                _ChildGender5[i] = "";
                _ChildNRIC5[i] = "";
                _ChildDOB5[i] = "";
                #endregion
            }
        }
        string _HeaderText = "";
        _HeaderText = "<table><tr><td colspan='" + _ProposalCount + 2 + "'><b>List of Policies Issued via CompareXpress</b></td></tr><tr><td colspan='" + _ProposalCount + 2 + "'></td></tr></table>";

        _html = "<table style='width: 100%; background-color: white;' cellpadding='0' cellspacing='0' border='1'>"
                + "<tr>"
                + "<td align='left' style='width:150px'>No</td>";
        for (int j = 1; j <= _ProposalCount; j++)
        {
            _html += "<td align='center' style='width:250px'>" + j + "</td>";
        }
        _html += "<td align='center' style='width:100px'>Total</td>"
        + "</tr>"

        + "<tr>"
        + "<td align='left'>Account Code</td>";
        for (int j = 1; j <= _ProposalCount; j++)
        {
            _html += "<td align='center'>" + _AccountCode[j - 1].ToString() + "</td>";
        }
        _html += "<td align='left'></td>"
        + "</tr>"

        + "<tr>"
        + "<td align='left'>Account/Agent Name</td>";
        for (int j = 1; j <= _ProposalCount; j++)
        {
            _html += "<td align='center'>" + _AcencyName[j - 1].ToString() + "</td>";
        }
        _html += "<td align='left'></td>"
        + "</tr>"


        + "<tr>"
        + "<td align='left'>Date/Time</td>";
        for (int j = 1; j <= _ProposalCount; j++)
        {
            _html += "<td align='center'>" + _PostDate[j - 1].ToString() + "</td>";
        }
        _html += "<td align='left'></td>"
        + "</tr>"


        + "<tr>"
        + "<td align='left'>Product Name</td>";
        for (int j = 1; j <= _ProposalCount; j++)
        {
            _html += "<td align='center'>" + _ProductName[j - 1].ToString() + "</td>";
        }
        _html += "<td align='left'></td>"
        + "</tr>"


        + "<tr>"
        + "<td align='left'>Plan-Type</td>";
        for (int j = 1; j <= _ProposalCount; j++)
        {
            _html += "<td align='center'>" + _PlanType[j - 1].ToString() + "</td>";
        }
        _html += "<td align='left'></td>"
        + "</tr>"


        + "<tr>"
        + "<td align='left'>Policy No</td>";
        for (int j = 1; j <= _ProposalCount; j++)
        {
            _html += "<td align='center'>" + _PolicyNo[j - 1].ToString() + "</td>";
        }
        _html += "<td align='left'></td>"
        + "</tr>"


        + "<tr>"
        + "<td align='left'>Period of Insurance From</td>";
        for (int j = 1; j <= _ProposalCount; j++)
        {
            _html += "<td align='center'>" + _PeriodFrom[j - 1].ToString() + "</td>";
        }
        _html += "<td align='left'></td>"
        + "</tr>"


        + "<tr>"
        + "<td align='left'>Period of Insurance To</td>";
        for (int j = 1; j <= _ProposalCount; j++)
        {
            _html += "<td align='center'>" + _PeriodTo[j - 1].ToString() + "</td>";
        }
        _html += "<td align='left'></td>"
        + "</tr>"


        + "<tr>"
        + "<td align='left'>AuCode</td>";
        for (int j = 1; j <= _ProposalCount; j++)
        {
            _html += "<td align='center'>" + _AuCode[j - 1].ToString() + "</td>";
        }
        _html += "<td align='left'></td>"
        + "</tr>"


        + "<tr>"
        + "<td align='left'> Before GST (SGD)</td>";
        for (int j = 1; j <= _ProposalCount; j++)
        {
            _html += "<td align='center'>" + _BeforeGST[j - 1].ToString() + "</td>";
        }
        _html += "<td align='left'>" + _AnnualPremium.ToString() + "</td>"
        + "</tr>"


        + "<tr>"
        + "<td align='left'>PH Name</td>";
        for (int j = 1; j <= _ProposalCount; j++)
        {
            _html += "<td align='center'>" + _PhName[j - 1].ToString() + "</td>";
        }
        _html += "<td align='left'></td>"
        + "</tr>"


        + "<tr>"
        + "<td align='left'>PH NRIC</td>";
        for (int j = 1; j <= _ProposalCount; j++)
        {
            _html += "<td align='center'>" + _PhNRIC[j - 1].ToString() + "</td>";
        }
        _html += "<td align='left'></td>"
        + "</tr>"


        + "<tr>"
        + "<td align='left'>PH D.O.B</td>";
        for (int j = 1; j <= _ProposalCount; j++)
        {
            _html += "<td align='center'>" + _PhDOB[j - 1].ToString() + "</td>";
        }
        _html += "<td align='left'></td>"
        + "</tr>"


        + "<tr>"
        + "<td align='left'>PH Occupation</td>";
        for (int j = 1; j <= _ProposalCount; j++)
        {
            _html += "<td align='center'>" + _PhOccupaion[j - 1].ToString() + "</td>";
        }
        _html += "<td align='left'></td>"
        + "</tr>"


        + "<tr>"
        + "<td align='left'>PH Mobile No</td>";
        for (int j = 1; j <= _ProposalCount; j++)
        {
            _html += "<td align='center'>" + _PhMobileNo[j - 1].ToString() + "</td>";
        }
        _html += "<td align='left'></td>"
        + "</tr>"


        + "<tr>"
        + "<td align='left'>PH House No</td>";
        for (int j = 1; j <= _ProposalCount; j++)
        {
            _html += "<td align='center'>" + _PhHouseNo[j - 1].ToString() + "</td>";
        }
        _html += "<td align='left'></td>"
        + "</tr>"


        + "<tr>"
        + "<td align='left'>Address</td>";
        for (int j = 1; j <= _ProposalCount; j++)
        {
            _html += "<td align='center'>#" + _PhUnitNo[j - 1].ToString() + " - " + _PhHomeNo[j - 1].ToString() + ",<br />" + _PhAddress[j - 1].ToString() + "</td>";
        }
        _html += "<td align='left'></td>"
        + "</tr>"


        + "<tr>"
        + "<td align='left'>Postal Code</td>";
        for (int j = 1; j <= _ProposalCount; j++)
        {
            _html += "<td align='center'>" + _PostalCode[j - 1].ToString() + "</td>";
        }
        _html += "<td align='left'></td>"
        + "</tr>"


        + "<tr>"
        + "<td align='left'>Spouse Name</td>";
        for (int j = 1; j <= _ProposalCount; j++)
        {
            _html += "<td align='center'>" + _SpouseName[j - 1].ToString() + "</td>";
        }
        _html += "<td align='left'></td>"
        + "</tr>"


        + "<tr>"
        + "<td align='left'>Spouse NRIC</td>";
        for (int j = 1; j <= _ProposalCount; j++)
        {
            _html += "<td align='center'>" + _SpouseNRIC[j - 1].ToString() + "</td>";
        }
        _html += "<td align='left'></td>"
        + "</tr>"


        + "<tr>"
        + "<td align='left'>Spouse D.O.B</td>";
        for (int j = 1; j <= _ProposalCount; j++)
        {
            _html += "<td align='center'>" + _SpouseDOB[j - 1].ToString() + "</td>";
        }
        _html += "<td align='left'></td>"
        + "</tr>"


        + "<tr>"
        + "<td align='left'>Spouse Occupation</td>";
        for (int j = 1; j <= _ProposalCount; j++)
        {
            _html += "<td align='center'>" + _SpouseOccupation[j - 1].ToString() + "</td>";
        }
        _html += "<td align='left'></td>"
        + "</tr>"


        + "<tr>"
        + "<td align='left'>Child 1 Name</td>";
        for (int j = 1; j <= _ProposalCount; j++)
        {
            _html += "<td align='center'>" + _ChildName1[j - 1].ToString() + "</td>";
        }
        _html += "<td align='left'></td>"
        + "</tr>"


        + "<tr>"
        + "<td align='left'>Child 1 Gender</td>";
        for (int j = 1; j <= _ProposalCount; j++)
        {
            _html += "<td align='center'>" + _ChildGender1[j - 1].ToString() + "</td>";
        }
        _html += "<td align='left'></td>"
        + "</tr>"


        + "<tr>"
        + "<td align='left'>Child 1 NRIC / BC No</td>";
        for (int j = 1; j <= _ProposalCount; j++)
        {
            _html += "<td align='center'>" + _ChildNRIC1[j - 1].ToString() + "</td>";
        }
        _html += "<td align='left'></td>"
        + "</tr>"


        + "<tr>"
        + "<td align='left'>Child 1 D.O.B</td>";
        for (int j = 1; j <= _ProposalCount; j++)
        {
            _html += "<td align='center'>" + _ChildDOB1[j - 1].ToString() + "</td>";
        }
        _html += "<td align='left'></td>"
        + "</tr>"


        + "<tr>"
        + "<td align='left'>Child 2 Name</td>";
        for (int j = 1; j <= _ProposalCount; j++)
        {
            _html += "<td align='center'>" + _ChildName2[j - 1].ToString() + "</td>";
        }
        _html += "<td align='left'></td>"
        + "</tr>"


        + "<tr>"
        + "<td align='left'>Child 2 Gender</td>";
        for (int j = 1; j <= _ProposalCount; j++)
        {
            _html += "<td align='center'>" + _ChildGender2[j - 1].ToString() + "</td>";
        }
        _html += "<td align='left'></td>"
        + "</tr>"


        + "<tr>"
        + "<td align='left'>Child 2 NRIC / BC No</td>";
        for (int j = 1; j <= _ProposalCount; j++)
        {
            _html += "<td align='center'>" + _ChildNRIC2[j - 1].ToString() + "</td>";
        }
        _html += "<td align='left'></td>"
        + "</tr>"


        + "<tr>"
        + "<td align='left'>Child 2 D.O.B</td>";
        for (int j = 1; j <= _ProposalCount; j++)
        {
            _html += "<td align='center'>" + _ChildDOB2[j - 1].ToString() + "</td>";
        }
        _html += "<td align='left'></td>"
        + "</tr>"


        + "<tr>"
        + "<td align='left'>Child 3 Name</td>";
        for (int j = 1; j <= _ProposalCount; j++)
        {
            _html += "<td align='center'>" + _ChildName3[j - 1].ToString() + "</td>";
        }
        _html += "<td align='left'></td>"
        + "</tr>"


        + "<tr>"
        + "<td align='left'>Child 3 Gender</td>";
        for (int j = 1; j <= _ProposalCount; j++)
        {
            _html += "<td align='center'>" + _ChildGender3[j - 1].ToString() + "</td>";
        }
        _html += "<td align='left'></td>"
        + "</tr>"


        + "<tr>"
        + "<td align='left'>Child 3 NRIC / BC No</td>";
        for (int j = 1; j <= _ProposalCount; j++)
        {
            _html += "<td align='center'>" + _ChildNRIC3[j - 1].ToString() + "</td>";
        }
        _html += "<td align='left'></td>"
        + "</tr>"


        + "<tr>"
        + "<td align='left'>Child 3 D.O.B</td>";
        for (int j = 1; j <= _ProposalCount; j++)
        {
            _html += "<td align='center'>" + _ChildDOB3[j - 1].ToString() + "</td>";
        }
        _html += "<td align='left'></td>"
        + "</tr>"


        + "<tr>"
        + "<td align='left'>Child 4 Name</td>";
        for (int j = 1; j <= _ProposalCount; j++)
        {
            _html += "<td align='center'>" + _ChildName4[j - 1].ToString() + "</td>";
        }
        _html += "<td align='left'></td>"
        + "</tr>"


        + "<tr>"
        + "<td align='left'>Child 4 Gender</td>";
        for (int j = 1; j <= _ProposalCount; j++)
        {
            _html += "<td align='center'>" + _ChildGender4[j - 1].ToString() + "</td>";
        }
        _html += "<td align='left'></td>"
        + "</tr>"


        + "<tr>"
        + "<td align='left'>Child 4 NRIC / BC No</td>";
        for (int j = 1; j <= _ProposalCount; j++)
        {
            _html += "<td align='center'>" + _ChildNRIC4[j - 1].ToString() + "</td>";
        }
        _html += "<td align='left'></td>"
        + "</tr>"


        + "<tr>"
        + "<td align='left'>Child 4 D.O.B</td>";
        for (int j = 1; j <= _ProposalCount; j++)
        {
            _html += "<td align='center'>" + _ChildDOB4[j - 1].ToString() + "</td>";
        }
        _html += "<td align='left'></td>"
        + "</tr>"


        + "<tr>"
        + "<td align='left'>Child 5 Name</td>";
        for (int j = 1; j <= _ProposalCount; j++)
        {
            _html += "<td align='center'>" + _ChildName5[j - 1].ToString() + "</td>";
        }
        _html += "<td align='left'></td>"
        + "</tr>"


        + "<tr>"
        + "<td align='left'>Child 5 Gender</td>";
        for (int j = 1; j <= _ProposalCount; j++)
        {
            _html += "<td align='center'>" + _ChildGender5[j - 1].ToString() + "</td>";
        }
        _html += "<td align='left'></td>"
        + "</tr>"


        + "<tr>"
        + "<td align='left'>Child 5 NRIC / BC No</td>";
        for (int j = 1; j <= _ProposalCount; j++)
        {
            _html += "<td align='center'>" + _ChildNRIC5[j - 1].ToString() + "</td>";
        }
        _html += "<td align='left'></td>"
        + "</tr>"


        + "<tr>"
        + "<td align='left'>Child 5 D.O.B</td>";
        for (int j = 1; j <= _ProposalCount; j++)
        {
            _html += "<td align='center'>" + _ChildDOB5[j - 1].ToString() + "</td>";
        }
        _html += "<td align='left'></td>"
        + "</tr>"
        + "</table>"
        + "<table style='width: 100%; background-color: white;' cellpadding='0' cellspacing='0' border='0'>";

        string date = string.Format("{0:dd}", CE.Global.General.GetUTCDateTime().Day.ToString("00"));
        date = date + "/" + string.Format("{0:MM}", CE.Global.General.GetUTCDateTime().Month.ToString("00"));
        date = date + "/" + string.Format("{0:yyyy}", CE.Global.General.GetUTCDateTime().Year.ToString());
        date = date + " " + CE.Global.General.GetUTCDateTime().ToShortTimeString();

        _html += "<tr><td colspan='" + _ProposalCount + 2 + "'></td></tr>"
        + "<tr><td colspan='" + _ProposalCount + 2 + "'>Total records generated : " + _ProposalCount + ".</td></tr>"
        + "<tr><td colspan='" + _ProposalCount + 2 + "'>Total Premium : " + _AnnualPremium.ToString() + "</td></tr>"
        + "<tr><td colspan='" + _ProposalCount + 2 + "'>Report generated on : " + date + "</td></tr>"
        + "<tr><td colspan='" + _ProposalCount + 2 + "'>Proposals are selected based on Policy Acceptance Date.</td></tr>"
        + "</table>";

        lblProposal.Text = _html;
        _fileName += ".xls";
        ExportGridView(_fileName, lblProposal, _HeaderText);

    }

    public void ExportGridView(string fileName, Label lblProposal, string headerText)
    {
        string attachment = "attachment; filename=" + fileName + ".xls";
        Response.ClearContent();
        Response.AddHeader("content-disposition", attachment);
        Response.ContentType = "application/ms-excel";
        StringWriter sw = new StringWriter();
        HtmlTextWriter htw = new HtmlTextWriter(sw);
        // Create a form to contain the grid
        HtmlForm frm = new HtmlForm();
        frm.InnerHtml = headerText;
        lblProposal.Parent.Controls.Add(frm);
        frm.Attributes["runat"] = "server";
        frm.Controls.Add(lblProposal);
        frm.RenderControl(htw);
        Response.Write(sw.ToString());
        Response.End();
    }

}
